import '../../../public.dart';

class BankCardSelectorWidget extends StatelessWidget {
  final WithdrawBankCardComboxDto? selectedBankCard;
  final ValueChanged<WithdrawBankCardComboxDto?> onBankCardChanged;
  final VoidCallback onTapSelector;
  final String? errorText;

  const BankCardSelectorWidget({
    super.key,
    required this.selectedBankCard,
    required this.onBankCardChanged,
    required this.onTapSelector,
    this.errorText,
  });

  @override
  Widget build(BuildContext context) {
    String? icon = CryptoUtil.decryptData(selectedBankCard?.iconUrl);
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      mainAxisSize: MainAxisSize.min,
      children: [
        Text(
          'Select Bank Card',
          style: TextStyle(
            fontSize: 16,
            fontWeight: FontWeight.w600,
            color: Colors.grey[700],
          ),
        ),
        const SizedBox(height: 8),
        GestureDetector(
          onTap: onTapSelector,
          child: Container(
            padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
            decoration: BoxDecoration(
              color: Colors.grey[50],
              borderRadius: BorderRadius.circular(8),
              border: Border.all(
                color: errorText != null
                    ? Colors.red.shade300
                    : Colors.grey.shade300,
              ),
            ),
            child: Row(
              children: [
                // Icon(Icons.account_balance, color: Colors.grey[500], size: 20),
                // 银行图标
                Container(
                  width: 32,
                  height: 32,
                  decoration: BoxDecoration(
                    color: Colors.white.withValues(alpha: 0.2),
                    borderRadius: BorderRadius.circular(8),
                  ),
                  child: icon != null
                      ? Image.network(
                          icon,
                          width: 24,
                          height: 24,
                          errorBuilder: (context, error, stackTrace) => Icon(
                            Icons.account_balance,
                            color: Colors.white,
                            size: 20,
                          ),
                        )
                      : Icon(
                          Icons.account_balance,
                          color: Colors.white,
                          size: 20,
                        ),
                ),
                const SizedBox(width: 12),
                Expanded(
                  child: selectedBankCard != null
                      ? Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Text(
                              selectedBankCard!.bankCardType ?? '',
                              style: const TextStyle(
                                fontSize: 14,
                                fontWeight: FontWeight.w500,
                                color: Colors.black87,
                              ),
                            ),
                            Text(
                              getBankNo(),
                              style: TextStyle(
                                fontSize: 12,
                                color: Colors.grey[600],
                              ),
                            ),
                          ],
                        )
                      : Text(
                          'Select bank card',
                          style: TextStyle(
                            color: Colors.grey[400],
                            fontSize: 14,
                          ),
                        ),
                ),
                Icon(
                  Icons.keyboard_arrow_down,
                  color: Colors.grey[400],
                  size: 20,
                ),
              ],
            ),
          ),
        ),
        if (errorText != null)
          Padding(
            padding: const EdgeInsets.only(top: 8),
            child: Text(
              errorText!,
              style: TextStyle(
                color: Colors.red.shade600,
                fontSize: 12,
                fontWeight: FontWeight.w500,
              ),
            ),
          ),
      ],
    );
  }

  String getBankNo() {
    return CryptoUtil.decryptData(selectedBankCard!.bankCard!) ?? '****';
  }
}
